//
//  BaseTabBarVC.m
//  GasCome
//
//  Created by zfc on 2020/1/7.
//  Copyright © 2020 zfc. All rights reserved.
//

#import "BaseTabBarVC.h"
#import "BaseNavigationVC.h"
#import "HomeVC.h"
#import "CompanyTotalVC.h"

@interface BaseTabBarVC ()<UITabBarControllerDelegate>

@end

@implementation BaseTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    [self addVC];
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTabbarSelectAction:) name:@"changeTabbarSelect" object:nil];
//
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notLoginAction) name:kLoginStatesNotLogin object:nil];

}

- (void) changeTabbarSelectAction:(NSNotification *)noti {
    NSDictionary *userInfo = noti.userInfo;
    
//    NSInteger index = [userInfo[@"index"] integerValue];
////    self.tabBar.
////    self.selectedIndex = index;
//    [self setSelectedIndex:index];
//    if (index == 3) {
//        BaseNavigationVC *naviController = self.childViewControllers[3];
//        MineVC *mineVc = naviController.childViewControllers[0];
//        [mineVc mineCell0BtnClickWithTag:0];
//    }
}


- (void) addVC {

    [self removeTabBarTopLine];

    self.tabBar.barTintColor = WhiteColor;
    self.tabBar.translucent = NO;

    self.tabBar.layer.shadowColor = [BlackColor CGColor];
    self.tabBar.layer.shadowOffset = CGSizeMake(0, -1);
    self.tabBar.layer.shadowOpacity = 0.1;
    self.tabBar.layer.cornerRadius = 5;
    self.tabBar.layer.shadowRadius = 5;
    self.tabBar.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.tabBar.bounds].CGPath;  //消除debugView警告

    //添加tabBar

    //首页
    HomeVC *homeVc = [[HomeVC alloc] init];
    [self addChildViewControllers:homeVc title:@"首页" image:@"nav_ic_shouye_0" selectImage:@"nav_ic_shouye_1"];

    //商城
//    StoreVC *storeVc = [[StoreVC alloc] init];
//    [self addChildViewControllers:storeVc title:@"商城" image:@"蒙版组 197" selectImage:@"蒙版组 193"];
    
    
    CompanyTotalVC *companyTotalVC = [[CompanyTotalVC alloc] init];
    [self addChildViewControllers:companyTotalVC title:@"运营" image:@"nav_ic_yunying_0" selectImage:@"nav_ic_yunying_1"];


}

//去除tabbar上方黑线
- (void)removeTabBarTopLine {
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.5);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, WhiteColor.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
}


- (void)addChildViewControllers:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage {
    UIImage *originalImage = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.image = originalImage;

    UIImage *originalSelectImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = originalSelectImage;

    childVc.tabBarItem.title = title;

    //文字渲染
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = Color80;
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    [childVc.tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    
    //选中的文字渲染
    dict[NSForegroundColorAttributeName] = Color4D;
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    [childVc.tabBarItem setTitleTextAttributes:dict forState:UIControlStateSelected];

    BaseNavigationVC *naviController = [[BaseNavigationVC alloc] initWithRootViewController:childVc];
    [self addChildViewController: naviController];

}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    NSInteger index = [self.childViewControllers indexOfObject:viewController];
    
    if (index == 0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"clickTabbar0" object:nil];
    }

    NSLog(@"点击了 %ld", (long)index);

//    if (index == 1) {
//        BaseNavigationVC *navc = self.childViewControllers[index];
//        StoreVC *vc = navc.childViewControllers[0];
//        [vc update];
//    }
    if ( index == 2 || index == 3) {
//        LoginModel *model = UserAccountManager.shared.user;
//        NSLog(@"%@", UserAccountManager.shared.user);
//        if(!UserAccountManager.shared.is_login){
//            [CommonFunc showLogin:viewController];
//            return NO;
//        }
        return YES;
    }




    return YES;

}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}



@end
